//
//  OneViewController.m
//  Study1
//
//  Created by ChenJie on 2/7/20.
//  Copyright © 2020 OrgName. All rights reserved.
//

#import "OneViewController.h"
#import "AView.h"

#define kLabelText @"三是各级妇幼保"
#define kLabelFont [UIFont systemFontOfSize:14]

@interface OneViewController ()
<
    AViewDelegate
>

@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) AView *aview;
@property (nonatomic, strong) UILabel *label;

@end

@implementation OneViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
    
    //
    self.navigationItem.rightBarButtonItem =
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                  target:self
                                                  action:@selector(doIt:)];
    
    
    [self.view addSubview:self.button];
    [self.view addSubview:self.aview];
//    [self.view addSubview:self.imageView];
//    self.imageView.image = [UIImage imageNamed:@"1"];
//    [self.view addSubview:self.leftButton];
//    [self.view addSubview:self.rightButton];
    
    [self.view addSubview:self.label];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    
    
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    //
    
    CGSize viewSize = self.view.frame.size;
    
    CGFloat topMark = 0;
    CGFloat spaceBetweenV = 10;
    
    CGFloat left = 10;
    CGFloat right = 10;
    CGFloat contentWidth = viewSize.width - left - right;
    CGFloat labelHeight = 44;
    CGFloat buttonHeight = 44;
//    CGFloat textFieldHeight = 44;
//    CGFloat textViewHeight = 50;
//    CGFloat segmentedHeight = 35;
//    CGFloat sliderHeight = 50;
//    CGFloat aViewWidth = 200;
    CGFloat aViewHeight = 200;
//
    //
    topMark = [UIApplication sharedApplication].statusBarFrame.size.height;
    topMark += self.navigationController.navigationBar.frame.size.height;
    topMark += 10;
    
//    CGSize labelSize = [kLabelText boundingRectWithSize:CGSizeMake(contentWidth, CGFLOAT_MAX)
//                                                options:NSStringDrawingUsesLineFragmentOrigin
//                                             attributes:@{NSFontAttributeName: kLabelFont}
//                                                context:nil].size;
//    labelHeight = labelSize.height;
//
//    self.label1111.frame = CGRectMake(left, topMark, contentWidth, labelHeight);
//    topMark += labelHeight;
    
    //
    topMark += spaceBetweenV;
    self.button.frame = CGRectMake(left, topMark, contentWidth, buttonHeight);
    topMark += buttonHeight;
    
    //
    topMark += spaceBetweenV;
//    self.imageView.frame = CGRectMake(left, topMark, imageViewWidth, imageViewHeight);
    self.aview.frame = CGRectMake(left, topMark, contentWidth, aViewHeight);
    topMark += aViewHeight;
    
    //
//    topMark += spaceBetweenV;
//    self.leftButton.frame = CGRectMake(left, topMark, contentWidth, buttonHeight);
//    topMark += buttonHeight;
//
//    //
//    topMark += spaceBetweenV;
//    self.rightButton.frame = CGRectMake(left, topMark, contentWidth, buttonHeight);
//    topMark += buttonHeight;
    
    topMark += spaceBetweenV;
    self.label.frame = CGRectMake(left, topMark, contentWidth, labelHeight);
    topMark += labelHeight;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Public methods



#pragma mark - Private methods



#pragma mark - Event response methods

- (void)buttonClicked:(UIButton *)button {
    NSLog(@"button clicked...");
}

- (void)doIt:(UIBarButtonItem *)buttonItem {
    NSLog(@"now click...");
    
    OneViewController *oneVC = [[OneViewController alloc] init];
    oneVC.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:oneVC animated:YES];
}

- (void)buttonDidiClick:(UIButton *)button {
    NSLog(@"sdfsf");
}

- (void)leftButtonClick:(UIButton *)button {
    
//    CGRect frame = self.imageView.frame;
//    frame.origin.x -= 10;
//    self.imageView.frame = frame;
    
//    [self.aview goLeft];
}

- (void)rightButtonClick:(UIButton *)button {
    NSLog(@"button clicked...");
//
//    CGRect frame = self.imageView.frame;
//    frame.origin.x += 10;
//    self.imageView.frame = frame;
    
//    [self.aview goRight];
}

#pragma mark - Deletate methods



#pragma mark - <AViewDelegate>

- (void)AViewLeftButtonWillClick:(AView *)view {
    NSLog(@"will go left..");
}

- (void)AViewLeftButtonDidClick:(AView *)view {
    NSLog(@"did go left..");
    
}

- (void)AViewRightButtonDidClick:(AView *)view {
    NSLog(@"go right..");
}

- (void)AView:(AView *)view frameDidChange:(CGRect)frame {
    self.label.text = NSStringFromCGRect(frame);
}

//- (UIImage *)iconForAview:(AView *)view {
//    return [UIImage imageNamed:@"2"];
//}

#pragma mark - Setter



#pragma mark - Getter

- (UIButton *)button {
    if (!_button) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //
        button.backgroundColor = [UIColor grayColor];
//        button.tag = ButtonTag_button1;
        
        //
        [button setTitle:@"Button" forState:UIControlStateNormal];
        [button setTitle:@"Button highLight" forState:UIControlStateHighlighted];
        [button setTitle:@"Button Selected" forState:UIControlStateSelected];
        
        //
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
        
        // target-action
        [button addTarget:self
                   action:@selector(buttonDidiClick:)
         forControlEvents:UIControlEventTouchUpInside];
        
        _button  = button;
    }
    return _button;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        imageView.backgroundColor = [UIColor lightGrayColor];
        
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.clipsToBounds = YES;
        
        _imageView = imageView;
    }
    return _imageView;
}

- (UIButton *)leftButton {
    if (!_leftButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //
        button.backgroundColor = [UIColor grayColor];
        [button setTitle:@"Left" forState:UIControlStateNormal];
        //
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        // target-action
        [button addTarget:self
                   action:@selector(leftButtonClick:)
         forControlEvents:UIControlEventTouchUpInside];
        
        _leftButton  = button;
    }
    return _leftButton;
}

- (UIButton *)rightButton {
    if (!_rightButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //
        button.backgroundColor = [UIColor grayColor];
        [button setTitle:@"Right" forState:UIControlStateNormal];
        //
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        // target-action
        [button addTarget:self
                   action:@selector(rightButtonClick:)
         forControlEvents:UIControlEventTouchUpInside];
        
        _rightButton  = button;
    }
    return _rightButton;
}

- (AView *)aview {
    if (!_aview) {
        AView *view = [[AView alloc] init];
        view.delegate = self;
        
        _aview = view;
    }
    return _aview;
}


- (UILabel *)label {
    if (!_label) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor lightGrayColor];
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentLeft;
        label.font = kLabelFont;
        
        _label = label;
    }
    return _label;
}

#pragma mark - Memory management

- (void)dealloc {
}

@end
